import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import("@/views/Login.vue")
const Home = () => import('@/views/home/Home')
const Users = () => import('@/views/users/Users')
const Roles = () => import('@/views/home/power/Roles')
const Rights = () => import('@/views/home/power/Rights')
const Cate = () => import('@/views/home/goods/Cate')
const Goods = () => import('@/views/home/goods/Goods')
const Params = () => import('@/views/home/goods/Params')

const routes = [
  {
    path:'',
    redirect:'/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path:'/home',
    name:'home',
    component: Home,
    redirect: '/users',
    children:[
      {path: '/users', component: Users},
      {path: '/roles', component: Roles},
      {path: '/rights', component: Rights},
      {path: '/categories', component: Cate},
      {path: '/goods', component: Goods},
      {path: '/params', component: Params}
    ]
  }
]
Vue.use(VueRouter)
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// //路由导航守卫
// router.beforeEach((to,from,next) =>{
//   if(to.path == '/login') return next()
//   const token = window.sessionStorage.getItem('token')
//   if(!token) next('/login')
//   next()
// })

//路由导航守卫
router.beforeEach((to,from,next) =>{
  if(to.path == '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if(!token) return next('/login')
  next()
})
export default router
